//
//  LWRegisterCtrl.m
//  laowu
//
//  Created by yinqy on 15/4/27.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//  质检员、施工员、业务员、业务经理、项目经理 注册页面

#import "LWRegisterZCtrl.h"
#import "JSRView.h"
#import "CustomSpannerView.h"

@interface LWRegisterZCtrl ()<CustomSpannerViewDelegate>


@property (weak, nonatomic) IBOutlet UILabel *jnLevelBtn;//技能等级
@property (weak, nonatomic) IBOutlet UILabel *capacityBtn;//资质


@property (weak, nonatomic) IBOutlet UITextField *sfzcodeField;//身份证号码

@property (weak, nonatomic) IBOutlet UITextField *pzjlField;//培训经历

@property (weak, nonatomic) IBOutlet UITextField *hkdzField;//户口地址

@property (weak, nonatomic) IBOutlet UITextField *czdField;//常住地

@property (weak, nonatomic) IBOutlet UITextField *wxhmField;//微信号码

@property (weak, nonatomic) IBOutlet UITextField *qqhmField;//QQ号码

@property (weak, nonatomic) IBOutlet UITextField *xgzxmField;//现在工作项目

@property (weak, nonatomic) IBOutlet UIButton *btnBusy;

@property (weak, nonatomic) IBOutlet UIButton *btnIdle;


@property (weak, nonatomic) IBOutlet UITextField *jsmgField;//推荐民工
@property (weak, nonatomic) IBOutlet UITextField *jsmgPhoneField;//推荐民工电话
@property (weak, nonatomic) IBOutlet UIView *containsView;
@property (weak, nonatomic) IBOutlet UIView *jsrview;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIButton *addMoreJSRBtn;
@property (weak, nonatomic) IBOutlet UIButton *enterBtn;








@property (nonatomic,weak)UIView *currentEditView;
@property (nonatomic,assign)CGFloat moveDis;//移动距离
@property (nonatomic,assign)CGFloat keyboardHeight;//键盘高度，如果为0，代表当前不是编辑模式

@property (nonatomic,strong)NSMutableArray *jsrChildViewArray;

@property (nonatomic,strong)NSDictionary *baseDic;

@end

@implementation LWRegisterZCtrl

-(instancetype)initWithDictionary:(NSDictionary *)dic{
    if (self=[super init]) {
        self.baseDic = dic;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"注册"];
    
    
    _jsrChildViewArray = [NSMutableArray array];
    
    [self.sfzcodeField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.pzjlField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.hkdzField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.czdField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.wxhmField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.qqhmField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.xgzxmField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.jsmgField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.jsmgPhoneField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    
    //添加键盘弹出与隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)textFieldBeginEditing:(UITextField *)textField{
    NSLog(@"textViewShouldBeginEditing");
    self.currentEditView = textField;
    if (self.keyboardHeight!=0) {
        [self adjustLocationWithKeyboardHeight:self.keyboardHeight];
    }
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.addMoreJSRBtn setY:CGRectGetMaxY(self.jsrview.frame)+15];
    [self.enterBtn setY:CGRectGetMaxY(self.addMoreJSRBtn.frame)+15];
    [self.scrollView setContentSize:CGSizeMake(SCREENW, CGRectGetMaxY(self.enterBtn.frame)+15)];
    
}

- (void)keyboardWillShow:(NSNotification *)notif
{
    //在键盘显示的时候去调整view
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    self.keyboardHeight = keyboardSize.height;
    [self adjustLocationWithKeyboardHeight:keyboardSize.height];
}

-(void)adjustLocationWithKeyboardHeight:(CGFloat)keyboardHeight{
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect editViewRect = [self.currentEditView convertRect:self.currentEditView.bounds toView:window];
    
    //首页判断键盘有没有遮挡view，如果没有遮挡，那个什么也不做
    CGFloat viewMaxY = CGRectGetMaxY(editViewRect);
    if (viewMaxY+10>SCREENH-keyboardHeight) {
        //代表键盘遮挡了view
        //需要调整uiscrollview的contentoffset移动
        self.moveDis = (viewMaxY+10)-(SCREENH-keyboardHeight);
        [UIView animateWithDuration:0.25 animations:^{
            [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y+self.moveDis)];
        }];
    }
}

-(void)keyboardWillHide:(NSNotification*)notif{
    self.keyboardHeight = 0;
    self.currentEditView = nil;
    if (self.scrollView.contentOffset.y!=0) {
        [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y-self.moveDis)];
    }
    self.moveDis = 0;
}

- (IBAction)statusBtnClick:(id)sender {
    UIButton *btn = sender;
    NSArray *stringArray = STRING_ARRAY_TECHDEGREE_STATUS;
    if(btn.tag == 1){
        //技能等级
        stringArray = STRING_ARRAY_SKILL_STATUS;
    }else if(btn.tag == 2){
        //资质
        stringArray = STRING_ARRAY_CAPACITY_STATUS;
    }
    CustomSpannerView *spannerView = [[CustomSpannerView alloc] initWithStringArray:stringArray bearingView:btn];
    [spannerView setDelegate:self];
    [spannerView show];
}

-(void)customSpannerItemClick:(NSString *)title index:(NSInteger)index bearingView:(UIView *)bearingView{
    if (bearingView.tag == 1) {
        [self.jnLevelBtn setText:title];
    }else if(bearingView.tag == 2){
        [self.capacityBtn setText:title];
    }
}


- (IBAction)enterBtnClick:(id)sender {
    if (ISBLANK(self.sfzcodeField.text)) {
        [DialogUtils showTipsWithView:nil String:@"请将数据填写完整"];
        return;
    }
    for (int i=0; i<self.jsrChildViewArray.count; i++) {
        JSRView *view = self.jsrChildViewArray[i];
        if (ISBLANK(view.nameField.text) || ISBLANK(view.phoneField.text)) {
            [DialogUtils showTipsWithView:nil String:@"请将数据填写完整"];
            return;
        }
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:self.baseDic];
    
    [param setObject:self.capacityBtn.text forKey:@"attr_p_z_aptitude"];//资质
    
    [param setObject:self.sfzcodeField.text forKey:@"attr_p_creditcard"];
    [param setObject:self.jnLevelBtn.text forKey:@"attr_p_skill_level"];
    
    [param setObject:self.pzjlField.text forKey:@"attr_p_train_exp"];
    [param setObject:self.hkdzField.text forKey:@"attr_p_address"];
    [param setObject:self.czdField.text forKey:@"attr_p_residence"];
    [param setObject:self.btnBusy.isSelected?@"忙":@"闲" forKey:@"attr_p_work_status"];
    [param setObject:self.wxhmField.text forKey:@"attr_p_wx"];
    [param setObject:self.qqhmField.text forKey:@"attr_p_qq"];
    [param setObject:self.xgzxmField.text forKey:@"attr_p_is_work_item"];
    [param setObject:self.jsmgField.text forKey:@"attr_p_intro_name"];
    [param setObject:self.jsmgPhoneField.text forKey:@"attr_p_intro_phone"];
    
//    for (int i=0; i<self.jsrChildViewArray.count; i++) {
//        JSRView *view = self.jsrChildViewArray[i];
//        [param setObject:view.nameField.text forKey:[NSString stringWithFormat:@"attr_p_introducer%d",i+2]];
//        [param setObject:view.phoneField.text forKey:[NSString stringWithFormat:@"attr_p_introducer%d_phone",i+2]];
//    }
    
    MBProgressHUD *HUD = [DialogUtils showLoadingWithView:nil];
    [MeDataTool registerWithParam:param url:@"api/z/join.json" success:^(LoginRes *result) {
        [HUD hide:YES];
        if (result.isSucc) {
            [DialogUtils showTipsWithView:nil String:@"注册成功"];
            [result.datas saveResult];
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [HUD hide:YES];
        [DialogUtils showTipsWithView:nil String:@"注册失败"];
    }];
    
}

- (IBAction)addMoreJSRBtnClick:(UIButton *)sender {
    CGFloat insetY = 0;
    if ([_jsrChildViewArray lastObject]) {
        UIView *tempView = [_jsrChildViewArray lastObject];
        insetY = CGRectGetMaxY(tempView.frame);
    }
    
    JSRView *jsrView = [[JSRView alloc] initWithFrame:CGRectMake(0, insetY, SCREENW, 66) nibNameOrNil:nil];
    [_jsrChildViewArray addObject:jsrView];
    [jsrView.nameField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [jsrView.phoneField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    
    
    
    [self.jsrview addSubview:jsrView];
    [self.jsrview setHeight:insetY+66];
    
    [self.addMoreJSRBtn setY:CGRectGetMaxY(self.jsrview.frame)+15];
    [self.enterBtn setY:CGRectGetMaxY(self.addMoreJSRBtn.frame)+15];
    [self.scrollView setContentSize:CGSizeMake(SCREENW, CGRectGetMaxY(self.enterBtn.frame)+15)];
}

- (IBAction)tap:(id)sender {
    [self.view endEditing:YES];
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

@end
